package j2EE.CLIENT;

import j2EE.factory.XMLParserFactory;
import j2EE.servlet.ResourcePool;
import lombok.extern.log4j.Log4j2;

import javax.xml.parsers.DocumentBuilder;

/**
 * Created with Intel IDEA.
 *
 * @Auther: gjt
 * @Date: 2024/11/30 16:36
 */

@Log4j2
public class XMLClient implements Runnable{
    private ResourcePool pool;
    public XMLClient (int poolSize) throws InterruptedException {
        pool = new ResourcePool(new XMLParserFactory(), poolSize);

        // 开始线程等待
        Thread thread = new Thread(this);
        thread.start();

        // 等待线程
        thread.join();

        // 清除
        thread.destroy();
    }


    @Override
    public void run() {
        DocumentBuilder resource;

        // 从池中获取XML 解析器
        try {
            resource = (DocumentBuilder) pool.getResource();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


        try{
            // 进行解析
        }catch (Exception er){
            log.error("异常");
        }finally {
            // 确保总是返回我们校验的资源
            pool.returnResource(resource);
        }
    }
}
